package com.cy.store.controller;

import com.cy.store.entity.User;
import com.cy.store.service.ex.*;
import com.cy.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.nio.file.attribute.UserDefinedFileAttributeView;

public class BaseControler {
    /*控制器类基类*/
    public static final int ok = 200;

    /** @ExceptionHandler用于统一处理方法抛出的异常 */
    @ExceptionHandler
    JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<Void>();
        if(e instanceof UsernameDuplicateException){
            result.setState(4000);
        }else if(e instanceof InsertException){
            result.setState(4001);
        }else if(e instanceof UserNotFoundException){
            result.setState(4002);
        }else if(e instanceof PasswordNotMatchException){
            result.setState(500);
        }else if(e instanceof UpdateException){
            result.setState(5001);
        }
        return result;
    }

    protected  final  Integer getUidFromSession(HttpSession session){
        return Integer.valueOf(session.getAttribute("uid").toString());
    }
    protected  final  String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }

}
